package com.renwei.yunlong.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.PasswordForGetBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ToastUtil;
import com.zyf.fwms.commonlibrary.http.Api;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.lang.ref.WeakReference;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 忘记密码页面
 * <p>
 * Created by Administrator on 2017/8/22.
 */

public class PasswordForGetActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.btn_determine)
    Button btnDetermine;

    static Button btnVercode;

    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.et_pwd)
    EditText etPwd;//密码
    @Bind(R.id.et_determine_pwd)
    EditText etDeterminePwd;//确认密码
    @Bind(R.id.et_verification)
    EditText etVerification;

    private HttpPresenter hp;//网络

    private PasswordHandler myHandler = new PasswordHandler(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {

        btnVercode = (Button) findViewById(R.id.btn_vercode);

        tvTitle.setText("忘记密码");
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        btnVercode.setOnClickListener(this);
        btnDetermine.setOnClickListener(this);
    }

    //业主忘记密码  发送验证码
    private void ownerVercodeHttp(String bean) {
        hp = new HttpPresenter(this);
        hp.setUrl("http://106.14.20.156:8083/AppCloudServer/sendVercode.htm");
        hp.setShowDialog(false);
        hp.setRequsetId(1);
        hp.putParam("jsonBean",bean);
        hp.setCallBack(this);
        hp.post();
    }

    //业主忘记密码
    private void ownerForgetPWDHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(this);
        hp.setUrl(Api.OWNER_FORGETPWD_URL);
        hp.setshowDialogcontent("正在提交");
        hp.setRequsetId(2);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    //服务商忘记密码  发送验证码
    private void serviceVercodeHttp(String jsonBean) {
        hp = new HttpPresenter(this);
        hp.setUrl("http://106.14.20.156:8083/AppServiceProvider/sendVercode.htm");
        hp.setShowDialog(false);
        hp.setRequsetId(3);
        hp.putParam("jsonBean",jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    //服务商忘记密码
    private void serviceForgetPWDHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(this);
        hp.setUrl(Api.SERVICE_FORGETPWD_URL);
        hp.setshowDialogcontent("正在提交");
        hp.setRequsetId(4);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_vercode:

                String phone = etPhone.getText().toString().trim();
                if (!TextUtils.isEmpty(phone)) {
                    if ("1".equals(getIntent().getStringExtra("companyType"))){
                        ownerVercodeHttp(phone);
                        sendMessageClick();//发送验证码
                    }else if ("2".equals(getIntent().getStringExtra("companyType"))){
                        serviceVercodeHttp(phone);
                        sendMessageClick();//发送验证码
                    }
                }
                break;
            case R.id.btn_determine:
                String mobilePhone = etPhone.getText().toString().trim();
                String vercode = etVerification.getText().toString().trim();
                String pwd = etDeterminePwd.getText().toString().trim();

                String jsonBean="{mobilePhone:\""+mobilePhone+"\",newPwd:\""+pwd+"\",vercode:\""+vercode+"\"}";

                if ("1".equals(getIntent().getStringExtra("companyType"))){
                    ownerForgetPWDHttp(jsonBean);
                }else if ("2".equals(getIntent().getStringExtra("companyType"))){
                    serviceForgetPWDHttp(jsonBean);
                }

                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {

        switch (requestId) {
            case 1:

                PasswordForGetBean bean = new Gson().fromJson(result, PasswordForGetBean.class);
                if (bean.getMessage().getCode() == 200) {
                    ToastUtil.showShort(this, "已发送");
                }
                break;
            case 2:

                Log.e("业主忘记密码返回",result.toString());

                bean = new Gson().fromJson(result, PasswordForGetBean.class);
                if (bean.getMessage().getCode() == 200) {
                    ToastUtil.showShort(this,"修改成功");
                    finish();
                }else {
                    ToastUtil.showShort(this,"验证码错误");
                }
                break;
            case 3:
                bean = new Gson().fromJson(result, PasswordForGetBean.class);
                if (bean.getMessage().getCode() == 200) {
                    ToastUtil.showShort(this, "已发送");
                }
                break;
            case 4:
                Log.e("服务商忘记密码返回",result.toString());

                bean = new Gson().fromJson(result, PasswordForGetBean.class);
                if (bean.getMessage().getCode() == 200) {
                    ToastUtil.showShort(this,"修改成功");
                    finish();
                }else {
                    ToastUtil.showShort(this,"验证码错误");
                }
                break;
            default:
                break;
        }

    }

    @Override
    public void onException(int requestId, String code) {

    }


    /**
     * 使用Handler
     * <p>
     * 静态内部类：避免内存泄漏
     */
    private static class PasswordHandler extends Handler {
        private final WeakReference<PasswordForGetActivity> weakReference;

        public PasswordHandler(PasswordForGetActivity activity) {
            weakReference = new WeakReference<PasswordForGetActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            PasswordForGetActivity activity = weakReference.get();
            if (activity != null) {
                switch (msg.what) {
                    case 0:
                        if (msg.arg1 == 0) {
                            btnVercode.setText("获取验证码");
                            btnVercode.setBackgroundColor(Color.parseColor("#689df8"));
                            btnVercode.setTextColor(Color.parseColor("#FFFFFF"));
                            btnVercode.setClickable(true);
                        } else {
                            btnVercode.setText("(" + msg.arg1 + ")秒");
                            btnVercode.setBackgroundColor(Color.parseColor("#b8bfcc"));
                            btnVercode.setClickable(false);
                        }
                        break;
                }
            }
        }
    }

    /**
     * 监听按钮下直接调用即可
     */
    private void sendMessageClick() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 59; i >= 0; i--) {
                    Message msg = myHandler.obtainMessage();
                    msg.arg1 = i;
                    myHandler.sendMessage(msg);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
